package com.zhang.webadmin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhang.webadmin.mapper.AclRoleMapper;
import com.zhang.webadmin.pojo.AclRole;
import com.zhang.webadmin.pojo.AclRolePermission;
import com.zhang.webadmin.service.AclRolePermissionService;
import com.zhang.webadmin.service.AclRoleService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.LinkedList;
import java.util.List;

/**
 * @author xx
 */
@Service
public class AclRoleServiceImpl extends ServiceImpl<AclRoleMapper, AclRole>
        implements AclRoleService {

    @Resource
    private AclRolePermissionService aclRolePermissionService;

    @Override
    public void setRolePermissions(Long roleId, List<Long> menuIdList) {
        
        // 先删除所有
        aclRolePermissionService.remove(
                new LambdaQueryWrapper<>(AclRolePermission.class)
                        .eq(AclRolePermission::getRoleId, roleId));
        // 在批量保存
        List<AclRolePermission> saveList = new LinkedList<>();

        for (Long menuId : menuIdList) {
            AclRolePermission add = new AclRolePermission();
            add.setRoleId(roleId);
            add.setPermissionId(menuId);
            add.setCreateTime(LocalDateTime.now());
            add.setUpdateTime(LocalDateTime.now());

            saveList.add(add);
        }

        aclRolePermissionService.saveBatch(saveList);
    }
}




